﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Microsoft.Win32;

namespace ExcelRefresh.Excel
{
    public static class Settings
    {
        private static RegistryKey BaseKey
        {
            // ReSharper disable PossibleNullReferenceException
            get
            {
                return Registry
                    .LocalMachine
                    .CreateSubKey(@"Software")
                    .CreateSubKey(@"Excel Refresher");
            }
            // ReSharper restore PossibleNullReferenceException
        }

        public static string[] FoldersToScan
        {
            get
            {
                return (string[])BaseKey.GetValue(@"Folders", new string[] { });
            }
            set
            {
                BaseKey.SetValue(@"Folders", value, RegistryValueKind.MultiString);
            }
        }

        public static int MaxThreads
        {
            get
            {
                return Convert.ToInt32(BaseKey.GetValue(@"MaxThreads", 3));
            }
            set
            {
                BaseKey.SetValue(@"MaxThreads", value, RegistryValueKind.QWord);
            }
        }

        public static TimeSpan RefreshTimeout
        {
            get
            {
                var seconds = Convert.ToInt32(BaseKey.GetValue(@"RefreshTimeout", 5 * 60));
                return TimeSpan.FromSeconds(seconds);
            }
            set
            {
                BaseKey.SetValue(@"RefreshTimeout",
                    Convert.ToInt32(value.TotalSeconds), 
                    RegistryValueKind.QWord);
            }
        }

        public static TimeSpan FileScanFrequency
        {
            get
            {
                var seconds = Convert.ToInt32(BaseKey.GetValue(@"FileScanFrequency", 2 * 60 * 60));
                return TimeSpan.FromSeconds(seconds);
            }
            set
            {
                BaseKey.SetValue(@"FileScanFrequency",
                    Convert.ToInt32(value.TotalSeconds),
                    RegistryValueKind.QWord);
            }
        }


        public static string CopyRefreshedFilesToFolder
        {
            get
            {
                return (string)BaseKey.GetValue(@"CopyRefreshedFilesToFolder", string.Empty);
            }
            set
            {
                BaseKey.SetValue(@"CopyRefreshedFilesToFolder", value, RegistryValueKind.String);
            }
        }

    }
}
